package org.summer.lawyer.controller.admin;

import cn.dev33.satoken.annotation.SaCheckPermission;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.summer.common.core.domain.R;
import org.summer.common.core.validate.AddGroup;
import org.summer.common.core.validate.EditGroup;
import org.summer.common.excel.utils.ExcelUtil;
import org.summer.common.idempotent.annotation.RepeatSubmit;
import org.summer.common.log.annotation.Log;
import org.summer.common.log.enums.BusinessType;
import org.summer.common.mybatis.core.page.PageQuery;
import org.summer.common.mybatis.core.page.TableDataInfo;
import org.summer.common.web.core.BaseController;
import org.summer.lawyer.domain.vo.feedback.FeedbackCreateVo;
import org.summer.lawyer.domain.vo.feedback.FeedbackUpdateVo;
import org.summer.lawyer.domain.vo.feedback.FeedbackVo;
import org.summer.lawyer.service.IFeedbackService;

import java.util.List;

/**
 * 意见反馈
 *
 * @author summer
 */
@Tag(name = "后台相关接口", description = "后台意见反馈相关接口")
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/lawyer/feedback")
public class FeedbackController extends BaseController {

    private final IFeedbackService feedbackService;

    /**
     * 查询意见反馈列表
     */
    @Operation(summary = "查询意见反馈列表", description = "查询意见反馈列表", responses = { @ApiResponse( responseCode = "200", description = "成功")})
    @SaCheckPermission("wxuser:feedback:list")
    @GetMapping("/list")
    public TableDataInfo<FeedbackVo> list(FeedbackVo vo, PageQuery pageQuery) {
        return feedbackService.selectPageList(vo, pageQuery);
    }

    /**
     * 导出意见反馈列表
     */
    @Operation(summary = "导出意见反馈列表", description = "导出意见反馈列表")
    @SaCheckPermission("wxuser:feedback:export")
    @Log(title = "意见反馈", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(FeedbackVo vo, HttpServletResponse response) {
        List<FeedbackVo> list = feedbackService.selectFeedbackVoList(vo);
        ExcelUtil.exportExcel(list, "意见反馈", FeedbackVo.class, response);
    }

    /**
     * 获取意见反馈详细信息
     *
     * @param id 主键
     */
    @Operation(summary = "查询意见反馈列表", description = "查询意见反馈列表", responses = { @ApiResponse( responseCode = "200", description = "成功")})
    @SaCheckPermission("wxuser:feedback:query")
    @GetMapping("/{id}")
    public R<FeedbackVo> getInfo(@NotNull(message = "主键不能为空")
                                 @PathVariable Long id) {
        return R.ok(feedbackService.selectFeedbackVoById(id));
    }

    /**
     * 新增意见反馈
     */
    @Operation(summary = "新增意见反馈", description = "新增意见反馈", responses = { @ApiResponse( responseCode = "200", description = "成功")})
    @SaCheckPermission("wxuser:feedback:add")
    @Log(title = "意见反馈", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping("/saveFeedback")
    public R<Void> saveFeedback(@Validated(AddGroup.class) @RequestBody FeedbackCreateVo vo) {
        return toAjax(feedbackService.insertByVo(vo));
    }

    /**
     * 修改意见反馈
     */
    @Operation(summary = "修改意见反馈", description = "修改意见反馈", responses = { @ApiResponse( responseCode = "200", description = "成功")})
    @SaCheckPermission("wxuser:feedback:edit")
    @Log(title = "意见反馈", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PostMapping("/updateFeedback")
    public R<Void> updateFeedback(@Validated(EditGroup.class) @RequestBody FeedbackUpdateVo vo) {
        return toAjax(feedbackService.updateByVo(vo));
    }

    /**
     * 删除意见反馈
     *
     * @param ids 主键串
     */
    @Operation(summary = "删除意见反馈", description = "删除意见反馈", responses = { @ApiResponse( responseCode = "200", description = "成功")})
    @SaCheckPermission("wxuser:feedback:remove")
    @Log(title = "意见反馈", businessType = BusinessType.DELETE)
    @DeleteMapping("/removeFeedback/{ids}")
    public R<Void> removeFeedback(@NotEmpty(message = "主键不能为空")
                                  @PathVariable Long[] ids) {
        return toAjax(feedbackService.deleteWithValidByIds(List.of(ids), true));
    }
}
